/*
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2024-04-15 14:29:31
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2024-12-18 14:28:24
 * @FilePath: \Backstage\Server\src\router\role.js
 */
const express = require('express');
const db = require('../utils/connDB');
const { verifyToken } = require('../utils/handleToken');
const { handleError } = require('../utils/common.js');

const router = express.Router();

router.get('/list', (req, res) => {
  const token = req.cookies['bs-cookie'];
  try {
    const user = verifyToken(token);
    if (!user) throw new Error('JsonWebTokenError:token验证失败');
    const { page, size, roleKey, name } = req.query;
    let offset = (page - 1) * size;
    let sql = `SELECT * FROM sys_role`;
    let lensql = `SELECT count('role_id') FROM sys_role`;

    if (name) {
      sql += ` WHERE role_name LIKE '%${name}%'`;
      lensql += ` WHERE role_name LIKE '%${name}%'`;
    }
    if (roleKey) {
      sql += ` ${name ? 'AND' : 'WHERE'} role_key LIKE '%${roleKey}%'`;
      lensql += ` ${name ? 'AND' : 'WHERE'} role_key LIKE '%${roleKey}%'`;
    }

    let pageSql = `${sql} ORDER BY role_sort ASC LIMIT ${size} OFFSET ${offset};`;
    let resuleSql = `${page && size ? pageSql : sql + ' ORDER BY role_sort ASC'}`;

    db.queryAsync(lensql).then(count => {
      db.queryAsync(resuleSql).then(result => {
        res.send({
          code: 200,
          data: result.results,
          msg: 'success',
          total: count.results[0]["count('role_id')"]
        });
      });
    });
  } catch (err) {
    handleError(res, err);
  }
});

// 角色编辑
router.put('/edit', (req, res) => {
  let token = req.cookies['bs-cookie'];
  try {
    const user = verifyToken(token);
    if (!user) throw new Error('JsonWebTokenError:token验证失败');
    if (user.role_key !== 'admin') throw new Error('ROLE_ERROR:权限不足');
    const { role_id, role_name, role_key, role_sort, remark, status } = req.body;
    const sql = `UPDATE sys_role SET role_name = '${role_name}', role_key = '${role_key}', role_sort = ${role_sort}, remark = '${remark}', status = ${status}, update_time = ?WHERE role_id = ${role_id}`;
    let params = [new Date()];
    db.queryAsync(sql, params)
      .then(result => {
        res.send({
          code: 200,
          data: result.results,
          msg: 'success'
        });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (err) {
    handleError(res, err);
  }
});

// 角色权限编辑
router.post('/auth/edit', (req, res) => {
  const token = req.cookies['bs-cookie'];
  try {
    const user = verifyToken(token);
    if (!user) throw new Error('JsonWebTokenError:token验证失败');
    if (user.role_key !== 'admin') throw new Error('ROLE_ERROR:权限不足');

    const { role_id, menu_ids } = req.body;
    const sql = `UPDATE sys_role SET menu_ids = '${menu_ids}', update_time = ?WHERE role_id = ${role_id}`;
    let params = [new Date()];
    db.queryAsync(sql, params)
      .then(result => {
        res.send({
          code: 200,
          data: result.results,
          msg: 'success'
        });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (error) {
    handleError(res, error);
    s;
  }
});

// 角色新增
router.post('/add', (req, res) => {
  const token = req.cookies['bs-cookie'];
  try {
    const user = verifyToken(token);
    if (!user) throw new Error('JsonWebTokenError:token验证失败');
    if (user.role_key !== 'admin') throw new Error('ROLE_ERROR:权限不足');
    const { role_name, role_key, role_sort, status, remark } = req.body;
    db.queryAsync(`SELECT* FROM sys_role where role_key = '${role_key}'`)
      .then(resKey => {
        if (resKey.results.length > 0) {
          return res.send({
            code: 500,
            data: [],
            msg: '角色key已存在'
          });
        }

        const sql = `INSERT INTO sys_role (role_name, role_key, role_sort, remark, status, create_time) VALUES ('${role_name}', '${role_key}', ${role_sort}, '${remark}', ?, ?)`;
        let params = [status, new Date()];
        db.queryAsync(sql, params)
          .then(result => {
            res.send({
              code: 200,
              data: result.results,
              msg: 'success'
            });
          })
          .catch(err => {
            handleError(res, err);
          });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (error) {
    handleError(res, error);
  }
});

// 角色删除
router.delete('/del/:id', (req, res) => {
  const token = req.cookies['bs-cookie'];
  try {
    const user = verifyToken(token);
    if (!user) throw new Error('JsonWebTokenError:token验证失败');
    if (user.role_key !== 'admin') throw new Error('ROLE_ERROR:权限不足');

    const { id } = req.params;
    if (id == 1 || id == 2) {
      return res.send({
        code: 500,
        data: [],
        msg: '系统角色, 不允许删除'
      });
    }
    const sql = `DELETE FROM sys_role WHERE role_id = ${id}`;
    db.queryAsync(sql)
      .then(result => {
        res.send({
          code: 200,
          data: result.results,
          msg: 'success'
        });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (error) {
    handleError(res, error);
  }
});

module.exports = router;
